//
//  MyViewController.m
//  KuWoCan
//
//  Created by hwd on 17/3/2.
//  Copyright © 2017年 hwd. All rights reserved.
//

#import "MyViewController.h"
#import "MyCell.h"
#import "AddressChooseVC.h"
#import "ChangePasswordVC.h"
#import "JinXCViewController.h"
#import "YaoHJHViewController.h"
#import "MyMessageViewController.h"
#import "MuBWCViewController.h"
#import "LoginViewController.h"
#import "MySpecialSignVC.h"
#import "DengJiViewController.h"
#import "CustomerViewController.h"
//Test
#import "XHMyTeamTableViewController.h"

#define TEST

#define kMineImgKey @"mineImg"

#define kMineTitleKey @"mineTitle"

#define kMineTypeKey @"mineType"

@interface MyViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong)UITableView *tableV;

@property (nonatomic,copy)NSArray *topImgArr;

@property (nonatomic,copy)NSArray *topTitleArr;

@property (nonatomic,strong) NSMutableArray *listShowArr;

@end

@implementation MyViewController
#pragma mark - life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    [self configUI];
    [self initializeListData];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    AppDelegate *applicationDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    [applicationDelegate.tabBarController hideTabBar:NO];
    self.navigationController.navigationBar.hidden = NO;

}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    AppDelegate *applicationDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    [applicationDelegate.tabBarController hideTabBar:YES];
}

#pragma mark - Config UI
- (void)configUI {
    self.title = @"我的";
    [self createRightButtonWithImage:[UIImage imageNamed:@"customer"] selector:@selector(CallPhone)];
    [self.view addSubview:self.tableV];
    [self.view setBackgroundColor:KwBgColor];
}
-(void)CallPhone//拨打客服
{
    [Constants callTelephoneWithPhone:@"4000716186" atViewController:self];
}
#pragma mark - Load data
- (void)loadBaseDate {
    _topImgArr = @[@"kuwo_logo"];
    _topTitleArr = @[@"职位",@"负责区域"];
}

- (void)initializeListData
{
    NSArray *imgArr;
    NSArray *titleArr;
    UserModel *user = [UserModel getUser];
    if ([user.userType integerValue] == kPersonTypeAgency)
    {
        imgArr = @[
                   @"01",
                   @"02",
                   @"03",
                   @"cus_09",
                   @"04",
                   @"05",
                   @"06",
                   @"07",
                   @"08",
                   @"level"
                   ];
        titleArr = @[     @"地址管理",
                          @"密码修改",
                          @"进销存管理",
                          @"客户管理",
                          @"要货计划",
                          @"我的资料",
                          @"目标完成",
                          @"我的团队",
                          @"我的签名",
                          @"我的等级"
                          ];


    }else
    {
                imgArr = @[
                            @"01",
                            @"02",
                            @"03",
                            @"04",
                            @"05",
                            @"06",
                            @"07",
                            @"08"
                            ];
            titleArr = @[     @"地址管理",
                              @"密码修改",
                              @"进销存管理",
                              @"要货计划",
                              @"我的资料",
                              @"目标完成",
                              @"我的团队",
                              @"我的签名"
                              ];

    }
   
    for (int i = 0; i<imgArr.count; i++)
    {
        NSMutableDictionary *tempDic = [NSMutableDictionary dictionary];
        [tempDic setValue:imgArr[i] forKey:kMineImgKey];
        [tempDic setValue:titleArr[i] forKey:kMineTitleKey];
        [tempDic setValue:@(i) forKey:kMineTypeKey];
        [self.listShowArr addObject:tempDic];
    }

    if ([user.userType integerValue] == kPersonTypeAgency)
    {
        NSMutableArray *arr = [NSMutableArray array];
        //经销商
        for (NSDictionary *tempDic in self.listShowArr)
        {
            if ([tempDic[kMineTitleKey] isEqualToString:@"我的团队"])
            {
                [arr addObject:tempDic];
            }
            else if ([tempDic[kMineTitleKey] isEqualToString:@"要货计划"])
            {
                [arr addObject:tempDic];
            }
            else
            {

            }
        }
        [self.listShowArr removeObjectsInArray:arr];
    }
    else
    {
        NSMutableArray *arr = [NSMutableArray array];
        for (NSDictionary *tempDic in self.listShowArr)
        {
            if ([tempDic[kMineTitleKey] isEqualToString:@"进销存管理"])
            {
                [arr addObject:tempDic];
            }
            else if ([tempDic[kMineTitleKey] isEqualToString:@"目标完成"])
            {
                [arr addObject:tempDic];
            }
            else if ([tempDic[kMineTitleKey] isEqualToString:@"地址管理"])
            {
                [arr addObject:tempDic];
            }
            else if ([tempDic[kMineTitleKey] isEqualToString:@"我的资料"])
            {
                [arr addObject:tempDic];
            }
//            else if ([tempDic[kMineTitleKey] isEqualToString:@"我的签名"])
//            {
//                [arr addObject:tempDic];
//            }
            else if ([tempDic[kMineTitleKey] isEqualToString:@"我的积分"])
            {
                [arr addObject:tempDic];
            }
            else
            {

            }
        }
        [self.listShowArr removeObjectsInArray:arr];
    }
    [self.tableV reloadData];
}

#pragma mark - Data request


#pragma mark - Registration notice and release


#pragma mark - UITableViewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }else
    {
        return self.listShowArr.count;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        return 80*screenW_6;
    }
    else
    {
        return 45*screenW_6;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.00001;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section == 0) {
        return 10*screenW_6;
    } else {
        return 70*screenW_6;
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    MyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[MyCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
    }
    if(indexPath.section ==  0)
    {
        [cell loadCellWith:nil];

        UserModel *user = [UserModel getUser];
        NSString *userTypeStr = [user userType];
        NSString *userName = [NSUserDefaults retrieveObjectForKey:@"realName"];
        NSString *userArea = [user areaName];
        NSString *userType = [NSString new];
        switch ([userTypeStr integerValue])
        {
            case kPersonTypeAgency:
            {
                userType = @"经销商";
            }
                break;
            case kPersonTypeAreaCharge:
            {
                userType = @"区域主管";
            }
                break;
            case kPersonTypeAreaManner:
            {
                userType = @"区域经理";
            }
                break;
            case kPersonTypeProvenceManner:
            {
                userType = @"省区经理";
            }
                break;

            default:
                break;
        }
        cell.ZhiWLab2.text = [NSString stringWithFormat:@"%@    %@",userType,userName];
        cell.fuZQYLab2.text = userArea;
        return cell;
    }else
    {
        [cell loadCell2];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; //右边小箭头
        NSDictionary *dic = self.listShowArr[indexPath.row];
        cell.titleLab.text = dic[kMineTitleKey];
        cell.imageView.image = kImg(dic[kMineImgKey]);
        cell.cellType = [dic[kMineTypeKey] integerValue];
        return cell;
    }

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MyCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if ([cell.titleLab.text isEqualToString:@"地址管理"]) {
        AddressChooseVC *adressVC = [[AddressChooseVC alloc]init];
        [self.navigationController pushViewController:adressVC animated:YES];
    }
    else if ([cell.titleLab.text isEqualToString:@"密码修改"])
    {
        ChangePasswordVC *adressVC = [[ChangePasswordVC alloc]init];
        [self.navigationController pushViewController:adressVC animated:YES];
    }
    else if ([cell.titleLab.text isEqualToString:@"进销存管理"])
    {
        JinXCViewController *adressVC = [[JinXCViewController alloc]init];
        [self.navigationController pushViewController:adressVC animated:YES];
    }
    else if ([cell.titleLab.text isEqualToString:@"要货计划"])
    {
        YaoHJHViewController *adressVC = [[YaoHJHViewController alloc]init];
        [self.navigationController pushViewController:adressVC animated:YES];
    }
    else if ([cell.titleLab.text isEqualToString:@"我的资料"])
    {
        MyMessageViewController *adressVC = [[MyMessageViewController alloc]init];
        [self.navigationController pushViewController:adressVC animated:YES];
    }
    else if ([cell.titleLab.text isEqualToString:@"目标完成"])
    {
        MuBWCViewController *adressVC = [[MuBWCViewController alloc]init];
        [self.navigationController pushViewController:adressVC animated:YES];

    }
    else if ([cell.titleLab.text isEqualToString:@"我的团队"])
    {
         [self.navigationController pushViewController:[XHMyTeamTableViewController new] animated:YES];
    }
    else if ([cell.titleLab.text isEqualToString:@"我的签名"])
    {
        MySpecialSignVC *adressVC = [[MySpecialSignVC alloc]init];
        [self.navigationController pushViewController:adressVC animated:YES];
    }
    else if ([cell.titleLab.text isEqualToString:@"我的等级"])
    {
        DengJiViewController *adressVC = [[DengJiViewController alloc]init];
        [self.navigationController pushViewController:adressVC animated:YES];
    }
    else if ([cell.titleLab.text isEqualToString:@"客户管理"])
    {
        CustomerViewController *adressVC = [[CustomerViewController alloc]init];
        [self.navigationController pushViewController:adressVC animated:YES];

    }
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    if (section == 1) {
        UIView *footView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENW, 150*screenW_6)];
        CanNButton *outBtn = [[CanNButton alloc]initWithFrame:CGRectMake(0, 15*screenW_6, SCREEN_WIDTH, 45*screenW_6)];
        [outBtn.layer setCornerRadius:5*screenW_6];
        outBtn.layer.masksToBounds = YES;
        [outBtn setTitle:@"退出登录" forState:UIControlStateNormal];
        outBtn.backgroundColor = [UIColor whiteColor];
        [outBtn setTitleColor:KwBlueColor forState:UIControlStateNormal];
        [outBtn addTarget:self action:@selector(signOut) forControlEvents:UIControlEventTouchUpInside];
        [footView addSubview:outBtn];
        return footView;
    }else
    {
        return nil;
    }

}


#pragma mark - event response
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1)
    {
        [Singleton shareInstance].isLogin = NO;
        [Singleton shareInstance].userId = @"";
        [Singleton shareInstance].userType = @"";

        UserModel *user = [UserModel getUser];
        [user deleteUser];

        //        //删除成功
        //        LoginViewController *loginVC = [[LoginViewController alloc] init];
        //        [self presentViewController:loginVC animated:YES completion:^{
        //
        //        }];
        [JPUSHService setTags:[NSSet setWithArray:@[]] alias:@"" fetchCompletionHandle:^(int iResCode, NSSet *iTags, NSString *iAlias){
            NSLog(@"rescode: %d, \ntags: %@, \nalias: %@\n", iResCode, iTags, iAlias);
        }];
        [SharedApp showLoginNav];
        [NSUserDefaults removeAll];
        [NSUserDefaults saveObject:@(YES) forKey:@"NotFirstUseApp"];
        [NSUserDefaults saveBool:YES forkey:@"netEnvironment"];
    }
}

#pragma mark - private method


#pragma mark - longout
- (void)signOut
{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"确定退出登录？" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alert show];
}

#pragma mark - getter and setter

-(NSMutableArray *)listShowArr
{
    if (!_listShowArr)
    {
        _listShowArr =[NSMutableArray array];
    }
    return _listShowArr;
}

- (UITableView *)tableV {
    if (!_tableV) {
        _tableV = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 60) style:UITableViewStylePlain];
        _tableV.backgroundColor = [UIColor colorWithHexString:@"F6F7F8"];
        _tableV.delegate = self;
        _tableV.dataSource = self;
        _tableV.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableV.showsVerticalScrollIndicator = NO;
        _tableV.showsHorizontalScrollIndicator = NO;
    }
    return _tableV;
}

@end
